package cn.fastoo.task;

import cn.fastoo.common.dao.BatchDao;
import cn.fastoo.common.dao.SmsrecordDao;
import cn.fastoo.common.dao.UserBaseDao;
import cn.fastoo.common.entity.Batch;
import cn.fastoo.common.entity.Smsrecord;
import cn.fastoo.common.entity.UserBase;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.service.JdbcTemplateService;
import cn.fastoo.service.UserService;
import cn.fastoo.common.utils.Constant;
import cn.fastoo.common.utils.JedisDao;
import cn.fastoo.common.utils.SmsSender;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import redis.clients.jedis.Jedis;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class TimingSendThread extends Thread {

    private final Logger logger = LoggerFactory.getLogger(TimingSendThread.class);

    private UserBaseDao userBaseDao;

    private BatchDao batchDao;

    private SmsrecordDao smsrecordDao;

    private UserService userService;

    private Long batchId;


    public TimingSendThread(UserBaseDao userBaseDao, BatchDao batchDao,SmsrecordDao smsrecordDao,UserService userService,Long batchId) {
        this.userBaseDao = userBaseDao;
        this.batchDao = batchDao;
        this.smsrecordDao = smsrecordDao;
        this.userService = userService;
        this.batchId = batchId;
    }



    @Override
    public void run() {
        Batch batch = batchDao.findById(batchId);
        if (batch==null) {
            return;
        }
        batch.setStatus(2); //标记为已发送状态
        batchDao.update(batch);


        List<Smsrecord> smsrecords = smsrecordDao.find(new Search().addSearchFilter("EQ_batchId",batchId).addSearchFilter("EQ_mtStatus",0));
        if (smsrecords!=null&&smsrecords.size()>0) {

            for (Smsrecord smsrecord : smsrecords) {
                if (smsrecord.getIsAudit() != 1) {
                    if (smsrecord.getIsAudit() == 0) {
                        logger.info("国内号码需要审核发送：" + smsrecord.getDestAddr());
                    } else {
                        logger.info("国内号码审核未通过禁止发送：" + smsrecord.getDestAddr());
                    }
                    continue;
                }

                //可以下发短信

                UserBase userBase = userBaseDao.findOne(new Search().addSearchFilter("EQ_loginName",smsrecord.getLoginName()));
                if (userBase==null) {
                    continue;
                }
                MtMessage mt = new MtMessage();
                mt.setDa(smsrecord.getDestAddr());
                mt.setMsg(smsrecord.getMsgContent());
                mt.setLocalMsgid(smsrecord.getLocalMsgid());
//                mt.setSignName(smsrecord.getSignName());
                mt.setApiKey(smsrecord.getApiKey());
                mt.setUserBaseId(userBase.getId());
                FastooMtMessageQueue.put(userService,mt);
            }
        }
    }



    public static void main(String[] ars) throws Exception{



    }
}
